<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />

<%@ page
	import="bibbletex.models.Collaboration,bibbletex.models.Reference,bibbletex.models.SessionData"
	import="bibbletex.controllers.ReferenceController, java.util.UUID, bibbletex.views.ReferenceView" %>

<%
	Collaboration c = sessiondata.collaboration;
	Reference r = null;
	
	String id_parameter = request.getParameter("rid");
	String rating_parameter = request.getParameter("rating");
	int rating = 0;
	if (rating_parameter != null)
		rating = Integer.parseInt(rating_parameter);
	
	if (c != null && id_parameter != null) {
		r = c.getAllReferences().get(
				c.getAllReferences().indexOf(
						new Reference(UUID.fromString(id_parameter))));
	
		if (rating_parameter != null){
			String cookiename = "referencerated";
			Cookie cookies[] = request.getCookies();
			boolean alreadyrated = false;
			if (cookies != null){
				for (int i = 0; i < cookies.length && !alreadyrated; i++){
					if (cookies[i].getName().equals(cookiename) 
						&& cookies[i].getValue().equals(r.getId().toString())){
						alreadyrated = true;
					}
				}
			}
			if (!alreadyrated){
				int ratingcounter = r.getRatingcounter();
				float newrating = (r.getRating()*ratingcounter + (float)rating)/(ratingcounter+1);
				r.setRatingcounter(ratingcounter+1);
				r.setRating(newrating);
				Cookie cookie = new Cookie (cookiename,r.getId().toString());
				cookie.setMaxAge(365 * 24 * 60 * 60);
				response.addCookie(cookie);
				ReferenceController.save(r);
				c.addReference(r);
			}
		}
		sessiondata.reference = r;
	}
%>

<span class="star1" <% if (sessiondata.collaboration_is_editable){ %>onmouseover="rateover('ratingr<%=r.getId().toString()%>', 1)" onmouseout="rateout('ratingr<%=r.getId().toString()%>', '<%=r.getRating() %>')" onclick="ajaxrate('<%=r.getId().toString()%>',1)"<%} else{%>style="cursor:default;"<%}%>><%=ReferenceView.star(r.getRating(), (float)0.5) %></span>
<span class="star2" <% if (sessiondata.collaboration_is_editable){ %>onmouseover="rateover('ratingr<%=r.getId().toString()%>', 2)" onmouseout="rateout('ratingr<%=r.getId().toString()%>', '<%=r.getRating() %>')" onclick="ajaxrate('<%=r.getId().toString()%>',2)"<%} else{%>style="cursor:default;"<%}%>><%=ReferenceView.star(r.getRating(), (float)1.5) %></span>
<span class="star3" <% if (sessiondata.collaboration_is_editable){ %>onmouseover="rateover('ratingr<%=r.getId().toString()%>', 3)" onmouseout="rateout('ratingr<%=r.getId().toString()%>', '<%=r.getRating() %>')" onclick="ajaxrate('<%=r.getId().toString()%>',3)"<%} else{%>style="cursor:default;"<%}%>><%=ReferenceView.star(r.getRating(), (float)2.5) %></span>
<span class="star4" <% if (sessiondata.collaboration_is_editable){ %>onmouseover="rateover('ratingr<%=r.getId().toString()%>', 4)" onmouseout="rateout('ratingr<%=r.getId().toString()%>', '<%=r.getRating() %>')" onclick="ajaxrate('<%=r.getId().toString()%>',4)"<%} else{%>style="cursor:default;"<%}%>><%=ReferenceView.star(r.getRating(), (float)3.5) %></span>
<span class="star5" <% if (sessiondata.collaboration_is_editable){ %>onmouseover="rateover('ratingr<%=r.getId().toString()%>', 5)" onmouseout="rateout('ratingr<%=r.getId().toString()%>', '<%=r.getRating() %>')" onclick="ajaxrate('<%=r.getId().toString()%>',5)"<%} else{%>style="cursor:default;"<%}%>><%=ReferenceView.star(r.getRating(), (float)4.5) %></span>
															
<%=Math.round(r.getRating() * 10) / (float)10 %>
